package cn.school.notice.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.notice.dao.WorkflowNoticeDao;
import cn.school.notice.entity.WorkflowNotice;
import cn.school.notice.service.WorkflowNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class WorkflowNoticeServiceImpl extends ServiceImpl<WorkflowNoticeDao, WorkflowNotice> implements WorkflowNoticeService {

    @Autowired
    private WorkflowNoticeDao tripWorkflowNoticeMapper;


    @Override
    public int insert(WorkflowNotice tripWorkflowNotice) {
        String userName = SysUserUtils.getSysUser().getUsername();
        tripWorkflowNotice.setCreateBy(userName);
        tripWorkflowNotice.setCreateTime(new Date());
        return tripWorkflowNoticeMapper.insert(tripWorkflowNotice);
    }

    @Override
    public int deleteNotice(WorkflowNotice tripWorkflowNotice) {
        int i = tripWorkflowNoticeMapper.deleteById(tripWorkflowNotice.getId());
        return i;
    }


    @Override
    public int update(WorkflowNotice tripWorkflowNotice) {
        return tripWorkflowNoticeMapper.updateById(tripWorkflowNotice);
    }

    @Override
    public List<WorkflowNotice> selectById() {
        return tripWorkflowNoticeMapper.selectList(new QueryWrapper<WorkflowNotice>());
    }

    @Override
    public IPage<WorkflowNotice> findNoticePageList(WorkflowNotice workflowNotice) {

        LambdaQueryWrapper<WorkflowNotice> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WorkflowNotice::getTenantId, SysUserUtils.getSysUser().getTenantId());
        String userName = SysUserUtils.getSysUser().getUsername();
        if(StringUtils.isEmpty(userName)){
            userName = SysUserUtils.getStudentUser().getStudentId();
        }
        wrapper.eq(WorkflowNotice::getNoticeUser,userName);
        wrapper.like(!StringUtils.isEmpty(workflowNotice.getTitle()),WorkflowNotice::getTitle, workflowNotice.getTitle());
        wrapper.like(!StringUtils.isEmpty(workflowNotice.getNoticeUser()),WorkflowNotice::getNoticeUser, workflowNotice.getNoticeUser());

        wrapper.orderByDesc(WorkflowNotice::getId);
        IPage<WorkflowNotice> page = page(new Page<>(workflowNotice.getPageNum(), workflowNotice.getPageSize()), wrapper);
        return page;
    }



}
